<?php
/**
 * Created by PhpStorm.
 * User: gyccvvh
 * Date: 2018-06-14
 * Time: 15:28
 */

namespace app\merchant\controller;


use app\merchant\service\Merchant;
use think\Controller;
use think\Request;

class Login extends App
{
    protected $mer_service;

    public function __construct(Request $request = null){
        parent::__construct($request);
        $this->mer_service = new Merchant();
    }

    public function login() {
        if($this->request->isPost()) {
            $_form = input('');
            $ret = $this->mer_service->login($_form);
            if($ret->getCode() == \app\merchant\define\Merchant::$Code_Mer_Login) {
                $merchant = $ret->getData("merchant");
                session("mer_id", $merchant['id']);
                $this->ret->setCode(0)->setUrl(url('/merchant/main'));
            }else {
                $this->ret->setCode($ret->getCode());
            }
            $this->ret->setMsg($ret->getMsg());
            return $this->fetch();
        }else {
            return $this->fetch();
        }
    }

    public function register() {
        if($this->request->isPost()) {
            $_form = select_object("username,password,email,mobile");
            $ret = $this->mer_service->register($_form);
            if($ret->getCode() == \app\merchant\define\Merchant::$Code_Mer_Register_Success) {
                $mer_id = $ret->getData("mer_id");
                session("mer_id",$mer_id);
                $this->ret->setCode(0)->setUrl(url('/merchant/main'));
            }else {
                $this->ret->setCode($ret->getCode());
            }
            $this->ret->setMsg($ret->getMsg());
            return $this->fetch();
        }else {
            return $this->fetch();
        }
    }
}